home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
501-525
/
disk_502
/
returncode
/
returncode.doc
< prev
next >
Wrap
Text File
|
1992-05-06
|
7KB
|
162 lines
LogRC and SetRC - tiny utilities
April 17, 1991 Jim Butterfield
(LogRC and SetRC will run on all Amiga systems, including
2.0; but LogRC will not be useful on systems prior to 1.3,
since the ENV: environment was not implemented before then).
Every CLI program leaves a Return Code - a number which says
how things went. (Workbench driven programs do, too, but I'm
not sure if the Return Code is used for anything).
A Return Code of 0 means: fine, things went OK.
A Return Code of 5 means: warning! some problem was seen,
but it didn't stop the program run.
A Return Code of 10 means: error! the program run was
impeded, but the program did struggle through.
A Return Code of 20 means: fail! the program couldn't run.
You'll find all this in the AmigaDOS User's Manual and elsewhere,
under IF and a bit more under FAILAT. The Return Code is mostly
of use with script files: IF can test for levels such as
WARN or ERROR, and FAILAT can decide that a script should give up
if it sees a specified Return Code level exceeded.
Chances are, you know all that. So why LogRC?
LogRC
-----
Here's the beef: the IF statement can test the Return Code if it
wishes; but it also CHANGES that value. IF is a program, too, and
it leaves its own Return Code. It doesn't HAVE to .. it could be
written to "pass through" the previous value. But .. no such luck.
One IF test, and the value is lost forever. (ELSE and ENDIF too).
So a little (or big) program might try its best to use a single
number to tell the script about how things went .. and the script
has ONE SHOT at seeing the number. It could use IF ERROR... but
once that's done, no point in trying IF WARN or IF FAIL.. the
Return Code has been lost and can't be tested again.
Should this be reported as a bug to be fixed? IF, ELSE, and ENDIF
could be changed to "pass through" the Return Code, unchanged.
LogRC and SetRC do that. Answer: probably not. IF and
company have worked that way since forever, and changing 'em now
might cause existing scripts to misfire. And the quick fix would
not please everyone .. it would be followed by requests to make
nested IF sections work properly with something akin to a "Return
Code stack" .. and that's just the start.
Instead, I thought I'd write an itty bitty program called LogRC
which would put the current Return Code into the environment
area. If you're working in CLI/Shell number 1, LogRC will put
that value into an environment variable called $Return1. Your
script may then test it repeatedly with syntax such as:
IF $Return<$$> EQ "WARN" ...
IF $Return<$$> EQ "ERROR" ...
The value in that environment variable won't change until you call
LogRC again. So test away to your heart's content.
Ummm .. both LogRC and SetRC are coded as "pure" programs.
Do you care? The BIG one is under 300 bytes long; conserving
RAM usage may not be your main mania in this case.
DOS 2.0 does give you an alternative. The Return Code (RC) becomes
available as a local variable, $RC, and Result2 as $Result2.
Again, you have ONE SHOT to grab one of these, using a command
such as SET MyRC $RC. You grab one, you lose the other.
About Return Code Levels.
----- ------ ---- ------
Environment variable $RETURN<$$> will contain a string value, a
word such as OK or WARN or FAIL. You can read it by commanding
"GetEnv Return1". You can also test with IF as indicated above.
A level of 0 translates to string "OK" in the environment variable.
A level of 5 to 9 becomes "WARN"; 10 to 19 shows as "ERROR"; and
20 and over, "FAIL". Levels 1 to 4 are not serious enough to be
even a WARN, but they are not zero, so they must be trying to tell
you something. I've called them MINOR for "minor glitch".
Although LogRC is intended for use mostly in scripts, you can
command LOGRC followed by GETENV RETURN1 (use the appropriate
CLI/Shell number). This will allow you to see results of a program
even when not running a script. (Or use SetRC, below).
A negative Return Code CAN be signalled by a program. More on this
in SetRC docs, following. But .. as far as I can tell, the
system throws these values away, and you'll never see a negative
Return Code.
SetRC
-----
This program was written to help investigate and test the Return
Code operation, so as to get LogRC into shape. I found it
interesting and useful in its own right.
SETRC <number> will set the Return Code to the number supplied.
The number should be supplied in decimal notation; negative numbers
should be prefixed with a minus sign. SETRC followed by nothing
does NOT set the Return Code; instead, it causes the current
Return Code to be printed.
Thus:
SETRC 0
SETRC
SETRC 55
SETRC -1
This last gives a curious result. You may know it already; if not,
try it.
The numeric value should be in the range -65536 to +65536, more or
less. I didn't bother putting in the extra code for longword values.
Values outside that range won't cause failure, but results may not
be what you intended.
Here's an oddity that I discovered with SETRC. Any program
returning a negative value will cause the previous Return Code to
remain unchanged. The negative value will NOT be logged. Commodore
docs ask you not to use negative values (well, -1 has a special
purpose which you'll discover if you try it, and maybe other values
will be tagged for specific roles in the future). But they don't
hurt anything at the moment.
GetReturn can be quite handy for testing a script file to see if
the "error coding" works as planned. Wanna fake a WARN level?
Just slip GETRETURN 5 into your script at the appropriate place,
and you've set it up. Remember: GetReturn does NOT use the
ENV: handler, and works on ALL Amigas.
Return Values
------ ------
Forgive me for belaboring the obvious, but perhaps some programmers
don't know this:
The Return Code is set within a C program by means of RETURN(n) ...
value n becomes the system Return Code.
The Return Code is set in Assembler by moving that value to the
D0 register just before terminating the program with RTS.
Result2: Future Feature?
-------- ---------------
A second value is often returned by programs, usually to signal how
Input/Output has behaved. This is "Result2"; if you see an error
notice on your program, it's probably generated by the Result2 value.
Command WHY also repeats Result2, translating its value into text for
you if command FAULT is available.
This set of programs does not reference Result2 at the moment.
It could be added; maybe that's a future project. Maybe not.
--Jim Butterfield